package com.qiguliuxing.dts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsManagerLog;
import com.qiguliuxing.dts.service.base.DtsManagerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/log")
@Validated
@Api(tags= ApiAdminConsts.Params.PARENT)
public class AdminLogController {
    private static final Logger logger = LoggerFactory.getLogger(AdminLogController.class);

    @Autowired
    private DtsManagerLogService dtsManagerLogService;

    @GetMapping("/list")
    @RequiresPermissions("admin:log:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "操作日志" }, button = "查询")
    @ApiOperation(value = ApiAdminConsts.Params.LIST)
    @WebLog(description = ApiAdminConsts.Params.LIST, excludes = {LogParamsConsts.RESPONSE})
    public Object list(@ApiParam(value = "操作人") String username,
                       @ApiParam(value = "操作接口") String methodDesc,
                       @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
                       @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
        List<DtsManagerLog> managerLogs = dtsManagerLogService.querySelective(username, methodDesc, page, limit, sort, order);
        long total = PageInfo.of(managerLogs).getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", managerLogs);
        return ResponseUtils.ok(data);
    }

    @PostMapping("/delete")
    @RequiresPermissions("admin:param:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "操作日志" }, button = "删除")
    @ApiOperation(value = ApiAdminConsts.Params.DELETE)
    @WebLog(description = ApiAdminConsts.Params.DELETE, persistence = true)
    public Object delete(@RequestBody DtsManagerLog managerLog) {
        dtsManagerLogService.deleteById(managerLog.getId());
        return ResponseUtils.ok();
    }
}
